#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <unistd.h>
#include <stdlib.h> 
#include <sys/wait.h>
#include <sstream>

using std::cout;
using std::endl;
using std::cin;
using std::string;
using std::ifstream;

int main()
{
	while (true)
	{
        cout<< "Enter a command:"<< endl;
        string s;
        cin>> s;
        
        int chpid;
        int pid;
        
        if (s.compare("version") == 0)
        {	    
             ifstream f;
             f.open ("/proc/version");
             while (f.good())
                cout << (char) f.get();
             f.close();
        }
        else if (s.compare("cpu") == 0)
        {	    
             ifstream f;
             f.open ("/proc/cpuinfo");
             while (f.good())
                cout << (char) f.get();
             f.close();
        }
        else if (s.compare("time") == 0)
        {	    
             ifstream f;
             f.open ("/proc/uptime");
             while (f.good())
                cout << (char) f.get();
             f.close();
        }
        else if (s.compare("exit") == 0)
        {            
            break;
        }
        else 
        {
            pid_t pid;
            pid = fork();
            int status;
            if (pid == -1) 
            {
                cout << "Error!" << endl;
                exit(pid);
            }             
            else if (pid == 0)
            {        
                if (s.compare("dir") == 0)                    
                        execlp("ls", "ls", (char *)NULL);     
                else 
                {
                    string param;
                    getline(cin, param);
                    char* keys[7];
                    keys[0] = const_cast<char*>(s.c_str());
                    int i = 1;
                    char* p = strtok (const_cast<char*>(param.c_str())," ");
                    while (p != NULL)
                    {
                            keys[i] = p;
                            p = strtok (NULL, " ");
                            ++i;
                    }
                    keys[i] = NULL;
                    execvp(keys[0], keys);
                }
            }
        }
     }     
     	
	return 0;
}
